package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MyZhiBoAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.ResponseBean;
import com.llkj.qingnianguoxue.bean.ZhiBoListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;
import com.llkj.qingnianguoxue.zhuanlan.BookZhiBoActivity;
import com.llkj.qingnianguoxue.zhuanlan.ZhiBoDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyZhiBoActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private static final int REQUEST_LOGIN_CODE = 9934;
    private static final int REQUEST_BOOK_CODE = 6789;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView lvZhiBo;
    private MyZhiBoAdapter zhiBoAdapter;//适配器
    private List<ZhiBoListBean.ZhiBoBean> zhiboData;//直播列表
    private int totalSize,mPage,mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_zhi_bo);
        setTitle("直播预约", true, TITLE_TYPE_IMG, R.drawable.icon_back, true,TITLE_TYPE_IMG, R.drawable.icon_book_share);
        ButterKnife.bind(this);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvZhiBo = (RecyclerView) findViewById(R.id.recyclerView);
        lvZhiBo.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        zhiboData = new ArrayList<>();
        zhiBoAdapter = new MyZhiBoAdapter(R.layout.item_zhibo, zhiboData);
        lvZhiBo.setAdapter(zhiBoAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        zhiBoAdapter.setOnLoadMoreListener(this);
        lvZhiBo.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ZhiBoListBean.ZhiBoBean zhiBoBean = zhiboData.get(i);
                int type = Integer.parseInt(zhiBoBean.getState());//状态1 直播中 2 未开始 3 已结束
                Bundle bundle = new Bundle();
                bundle.putString("id",zhiBoBean.getId());
                switch (type) {
                    case 1:
                        bundle.putString("type", "1");
                        startActivity(ZhiBoDetailActivity.class, bundle);
                        break;
                    case 2:
                        startActivityForResult(BookZhiBoActivity.class, bundle, REQUEST_BOOK_CODE);
                        break;
                    case 3:
                        bundle.putString("type", "2");
                        startActivity(ZhiBoDetailActivity.class, bundle);
                        break;
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_BOOK_CODE) {

            }
        }else if(resultCode == RESULT_OK && requestCode == REQUEST_LOGIN_CODE){
            onRefresh();
        }
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, zhiBoAdapter, null, lvZhiBo, totalSize);
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivityForResult(LoginActivity.class, new Bundle(),REQUEST_LOGIN_CODE);
    }


    private void getData(){
//        if(mPage==1){
//            try {
//                handler.removeCallbacks(runnable);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            loadWaitDialog();
//        }
        HttpMethod.liveIndex(this,this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(method == HttpMethod.HTTP_LIVE_INDEX){//7.1 直播首页
            ZhiBoListBean zhiBoListBean = GsonUtil.GsonToBean(data, ZhiBoListBean.class);
            if(zhiBoListBean != null){
                totalSize = Integer.parseInt(zhiBoListBean.getNum());
                if (mPage == 1) {
                    zhiBoAdapter.getCountDownList().clear();
                    zhiboData.clear();
                    LoadDataHelper.refreshComplete(zhiBoAdapter, zhiboData, refreshLayout, lvZhiBo);
                } else {
                    zhiBoAdapter.setEnableLoadMore(true);
                    zhiBoAdapter.loadMoreComplete();
                }
                zhiboData.addAll(zhiBoListBean.getList());
                zhiBoAdapter.notifyDataSetChanged();

//                handler.postDelayed(runnable, 1000);
            }
        }else if(method == HttpMethod.HTTP_LIVE_ORDER){//7.2直播预约
            ToastUtil.makeLongText(this, "预约成功");
            ResponseBean responseBean = GsonUtil.GsonToBean(data,ResponseBean.class);
            if(responseBean != null){
                String time = responseBean.getResidue_time();
                ZhiBoListBean.ZhiBoBean item = zhiboData.get(mCurrentPosition);
                item.setIs_order("1");
                item.setCountdown(time);
                zhiBoAdapter.notifyItemChanged(mCurrentPosition);
            }
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this, message);
        }
    }

//    Handler handler = new Handler();
//    Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
//            ZhiBoListBean.ZhiBoBean item = null;
//            for(int i=0;i<zhiboData.size();i++){
//                item = zhiboData.get(i);
//                if("1".equals(item.getIs_order())){
////                    zhiBoAdapter.notifyItemChanged(i);
//                    try {
//                        String time = zhiBoAdapter.calculateTime(i,item);
//                        zhiBoAdapter.getCountDownList().get(i).setText(time);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//            handler.postDelayed(this, 1000);
//        }
//    };
}
